import panel as pn
import pandas as pd
import polars as pl
from datetime import date, timedelta
from db import (
    DuckDBHelper,
    insert_journal_info,
    insert_admission_info,
    insert_discharge_info,
    query_daily_ward_indicators,
    query_admission_info_mssql,
    query_discharge_info_mssql,
    merge_journal_info,
    query_month_journal
)

pn.extension('modal',notifications=True, sizing_mode="stretch_width")
# 调用月志查询函数
def get_month_journal(date_range):
    start_date, end_date = date_range
    df = query_month_journal(start_date, end_date)
    if df is None:
        return None
    df = df.to_pandas()
    return df

# 回调,导入入院数据
def import_admission_data(event):
    if not event:
        return
    start_date, end_date = btn_date_import.value
    start_date = start_date.strftime('%Y-%m-%d')
    end_date = end_date.strftime('%Y-%m-%d 23:59:59')
    try:
        insert_admission_info(start_date, end_date)
        result_message(f'入院数据导入成功: {start_date} 至 {end_date}')
        # pn.state.notifications.success(f'入院数据导入成功: {start_date} 至 {end_date}')
    except Exception as e:
        pn.state.result_message(f'入院数据导入失败: {e}')

# 回调,导入出院数据
def import_discharge_data(event):
    if not event:
        return
    start_date, end_date = btn_date_import.value
    start_date = start_date.strftime('%Y-%m-%d')
    end_date = end_date.strftime('%Y-%m-%d 23:59:59')
    try:
        insert_discharge_info(start_date, end_date)
        result_message(f'出院数据导入成功: {start_date} 至 {end_date}')
    except Exception as e:
        result_message(f'出院数据导入失败: {e}')

# 生成临床日志
def generate_journal(event):
    if not event:
        return
    start_date, end_date = btn_date_import.value
    start_date = start_date.strftime('%Y-%m-%d')
    end_date = end_date.strftime('%Y-%m-%d 23:59:59')
    try:
        insert_journal_info(start_date, end_date)
        result_message(f'临床日志生成成功: {start_date} 至 {end_date}')
    except Exception as e:
        result_message(f'临床日志生成失败: {e}')

# 消息显示
def result_message(message:str=' '):
    return pn.pane.Markdown(f"### {message}")

# 当前日期
today = date.today()

first_day_this_month = date(today.year, today.month, 1)
last_day_last_month = first_day_this_month - timedelta(days=1)
first_day_last_month = date(last_day_last_month.year, last_day_last_month.month, 1)

btn_date_data = pn.widgets.DateRangePicker(
    name='选择日期范围:', value=(first_day_last_month, last_day_last_month),width=400
)
get_month_journal_bind=pn.bind(get_month_journal, btn_date_data)

btn_date_import = pn.widgets.DateRangePicker(
    name='日期范围:', value=(first_day_last_month, last_day_last_month)
)

start_date, end_date = btn_date_data.value
start_date = start_date.strftime('%Y-%m-%d')
end_date = end_date.strftime('%Y-%m-%d 23:59:59')

# start_date = '2025-08-01'  # 使用标准日期格式
# end_date = '2025-08-31 23:59:59'    # 函数内部会自动添加时间部分

btn_import_admission=pn.widgets.Button(name='1 导入入院数据',button_type='primary',width=100,align='center')
btn_import_discharge=pn.widgets.Button(name='1 导入出院数据',button_type='primary',width=100,align='center')
btn_generator_journal=pn.widgets.Button(name='2 生成临床日志',button_type='primary',height=70,align='center')
import_admission_bind=pn.bind(import_admission_data,btn_import_admission,watch=True)
import_discharge_bind=pn.bind(import_discharge_data,btn_import_discharge,watch=True)
generator_journal_bind=pn.bind(generate_journal,btn_generator_journal,watch=True)

modal=pn.Modal(
    pn.pane.Markdown("### 导入\n\n"),  
    btn_date_import,  
    pn.Row(
        pn.Column(btn_import_admission,btn_import_discharge),
        pn.Column(btn_generator_journal),align='center',margin=20
        ), 
    pn.Row(result_message,align='center'),   
    name='选择日期范围',margin=20,width=500,height=400
)
modal.controls(jslink=True)
btn_modal=modal.create_button("toggle",name='导入入院/出院数据',button_type='primary',width=150,align='end')

btn_row=pn.Row(btn_modal)
picker_row=pn.Row(btn_date_data)

journal_row=pn.Row(get_month_journal_bind)

pn.Column(btn_row,picker_row,journal_row,modal).servable()


